using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Security.Cryptography;
using PlugNT.Safe;
using PlugNT.Database.Common;
using PlugNT.Database.Common.Simple;
using PlugNT.Cms.Custom;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.DAL
{
    public class Field
    {

        private static string currTableName = WebConfig.TablePrefix + "field";











        public FieldInfo GetFieldInfo(int id)
        {
            FieldInfo model = null;
            IDataReader dr = GetReader(1, "*", " and id=" + id);
            if (dr != null)
            {
                if (dr.Read())
                {
                    model = new FieldInfo();
                    model.id = int.Parse(dr["id"].ToString());
                    model.sys_model_id = int.Parse(dr["sys_model_id"].ToString());
                    model.title = dr["title"].ToString();
                    model.message = dr["message"].ToString();
                    model.html_type = dr["html_type"].ToString();
                    model.html_depth = dr["html_depth"].ToString();
                    model.default_value = dr["default_value"].ToString();
                    model.category_no_list = dr["category_no_list"].ToString();
                    model.is_sub_category = dr["is_sub_category"].ToString() == "1";
                    model.column = dr["column"].ToString();
                    model.column_type = dr["column_type"].ToString();
                    model.order = int.Parse(dr["order"].ToString());
                    model.length = int.Parse(dr["length"].ToString());
                }
                dr.Close();

            }
            return model;
        }

        public string[] GetFieldArr(int sysModelId,string colName)
        {
            string[] arr = null;
            IDataReader dr = GetReader(1, "*", " and sys_model_id=" + sysModelId + " and column='" + StringHelper.SqlFilter(colName) + "'");
            if (dr != null)
            {
                if (dr.Read())
                {
                    arr = new string[2];
                    arr[0] = dr["category_no_list"].ToString();
                    arr[1] = dr["is_sub_category"].ToString();
                }
                dr.Close();

            }
            return arr;
        }










        public bool Add(FieldInfo model)
        {


            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + currTableName + "(");
            strSql.Append("[sys_model_id],[column_index],[title],[message],html_type,[html_depth],[default_value],[category_no_list],[is_sub_category],[column],[column_type],[order],[length]");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(" " + model.sys_model_id + ",");
            strSql.Append("'" + model.sys_model_id.ToString() + PlugNT.Safe.StringHelper.SqlFilter(model.column) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.title) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.message) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.html_type) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.html_depth) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.default_value) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.category_no_list) + "',");
            strSql.Append(" " + (model.is_sub_category?"1":"0") + ",");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.column) + "',");
            strSql.Append("'" + PlugNT.Safe.StringHelper.SqlFilter(model.column_type) + "',");
            strSql.Append(" " + model.order.ToString() + ",");
            strSql.Append(" " + model.length.ToString());
            strSql.Append(")");


            bool flag = false;
            try
            {
                flag = DbHelper.ExecuteSql(strSql.ToString()) > 0;
            }
            catch(Exception ex) 
            {
                SysManager.LogErrorWriteln(strSql.ToString() + "\n" + ex.Message + "\n" + ex.StackTrace);
            }
            return flag;
        }






        public bool Update(FieldInfo model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + currTableName + " set ");
            strSql.Append("[column_index]='" + model.sys_model_id.ToString() + PlugNT.Safe.StringHelper.SqlFilter(model.column) + "',");
            strSql.Append("[title]='" + PlugNT.Safe.StringHelper.SqlFilter(model.title) + "',");
            strSql.Append("[message]='" + PlugNT.Safe.StringHelper.SqlFilter(model.message) + "',");
            strSql.Append("[html_type]='" + PlugNT.Safe.StringHelper.SqlFilter(model.html_type) + "',");
            strSql.Append("[html_depth]='" + PlugNT.Safe.StringHelper.SqlFilter(model.html_depth) + "',");
            strSql.Append("[default_value]='" + PlugNT.Safe.StringHelper.SqlFilter(model.default_value) + "',");
            strSql.Append("[category_no_list]='" + PlugNT.Safe.StringHelper.SqlFilter(model.category_no_list) + "',");
            strSql.Append("[is_sub_category]=" + (model.is_sub_category ? "1" : "0") + ",");
            strSql.Append("[column]='" + PlugNT.Safe.StringHelper.SqlFilter(model.column) + "',");
            strSql.Append("[column_type]='" + PlugNT.Safe.StringHelper.SqlFilter(model.column_type) + "',");
            strSql.Append("[order]=" + model.order.ToString() + ",");
            strSql.Append("[length]=" + model.length.ToString());
            strSql.Append(" where [id]=" + model.id.ToString());

            if (DbHelper.ExecuteSql(strSql.ToString()) > 0)
            {
                FieldSearch.Update(model.sys_model_id, model.column, model.category_no_list, model.is_sub_category);
                return true;
            }
            else
            {
                return false;
            }
        }









        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + currTableName);
            strSql.Append(" where id=" + id.ToString());
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }













        private IDataReader GetReader(int topCount, string colNames, string andWhere)
        {
            string sql = "select " + (topCount == -1 ? "" : "top " + topCount) + " " + colNames + " from " + currTableName + " where 1=1 " + andWhere;
            return DbHelper.ExecuteReader(sql);
        }






        public static DataTable GetList(int sysModelId, string andWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from  [" + currTableName + "] where sys_model_id= " + sysModelId + andWhere);
            if (WebConfig.IsAccess)
            {
                strSql.Append(" Order By [order],id Asc");
            }
            else
            {
                strSql.Append(" Order By [order] Asc");
            }
            return DbHelper.GetDataTable(strSql.ToString());
        }








        public DataTable GetBindList(int sysModelId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select a.*,b.name from  [" + currTableName +
                "] as a left join " +SysModel.currTableName +
                " as b on a.sys_model_id=b.id where a.sys_model_id=" + sysModelId );
            if (WebConfig.IsAccess)
            {
                strSql.Append(" Order By a.[order],a.id Asc");
            }
            else
            {
                strSql.Append(" Order By a.[order] Asc");
            }

            return DbHelper.GetDataTable(strSql.ToString());
        }








        public int GetCount(int sysModelId, string andWhere)
        {
            string sql = "select count(1) from [" + currTableName + "] where  and sys_model_id= " + sysModelId + andWhere;
            return (int)DbHelper.GetSingle(sql);
        }







        public bool Exists(int sysModelId, string colName)
        {
            if (colName != "")
            {
                if (WebConfig.IsAccess)
                {




                    return false;
                }
                else
                {
                    return DbHelper.ExistsColumn(Article.currTableName + sysModelId, PlugNT.Safe.StringHelper.SqlFilter(colName));
                }
            }
            else
            {
                return false;
            }
        }


    }
}

